package cn.tedu.store.product.webapi.service.impl;

import cn.tedu.store.commons.pojo.product.dto.SkuAddNewDTO;
import cn.tedu.store.commons.pojo.product.dto.SkuUpdateFullInfoDTO;
import cn.tedu.store.commons.pojo.product.entity.Sku;
import cn.tedu.store.commons.pojo.product.entity.SkuSpecification;
import cn.tedu.store.commons.pojo.product.vo.SkuListItemVO;
import cn.tedu.store.commons.pojo.product.vo.SkuStandardVO;
import cn.tedu.store.commons.ex.ServiceException;
import cn.tedu.store.product.webapi.mapper.SkuMapper;
import cn.tedu.store.product.webapi.mapper.SkuSpecificationMapper;
import cn.tedu.store.product.service.ISkuService;
import cn.tedu.store.product.webapi.util.IdUtils;
import cn.tedu.store.commons.web.ServiceCode;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@DubboService
public class SkuServiceImpl implements ISkuService {

    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private SkuSpecificationMapper skuSpecificationMapper;

    @Override
    public void addNew(SkuAddNewDTO skuAddNewDTO) {
        log.debug("开始处理[添加sku]业务 参数:{}", skuAddNewDTO);
        Long skuId = IdUtils.getId();
        Sku sku = new Sku();
        BeanUtils.copyProperties(skuAddNewDTO, sku);
        sku.setId(skuId);
        int rows = skuMapper.insert(sku);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再次尝试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }

        String skuSpecification = skuAddNewDTO.getSpecifications();
        List<SkuSpecification> list = JSON.parseArray(skuSpecification, SkuSpecification.class);
        if (!list.isEmpty()) {
            for (SkuSpecification specification : list) {
                log.debug("skuSpecification :{}", specification);
                specification.setId(skuId);
            }
            skuSpecificationMapper.insertBatch(list);
        }
    }

    @Override
    public void updateFullInfoById(Long id, SkuUpdateFullInfoDTO skuUpdateFullInfoDTO) {
        log.debug("开始处理[修改sku信息]业务 参数:{},{}", id, skuUpdateFullInfoDTO);
        SkuStandardVO result = skuMapper.getStandardById(id);
        if(result == null){
            String message = "修改失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        Sku sku = new Sku();
        BeanUtils.copyProperties(skuUpdateFullInfoDTO,sku);
        sku.setId(id);
        int rows = skuMapper.updateById(sku);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再次尝试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

    }

    @Override
    public void reduceStockById(Long id, Integer stock) {
        Integer oldStock = skuMapper.getStandardById(id).getStock();
        if(oldStock < stock){
            String message = "库存不足!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        int rows = skuMapper.updateStockById(id, stock);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再次尝试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public SkuStandardVO getStandardById(Long id) {
        log.debug("开始处理[获取sku详情]业务 参数:{}", id);
        SkuStandardVO result = skuMapper.getStandardById(id);
        if(result == null){
            String message = "获取失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return result;
    }

    @Override
    public List<SkuListItemVO> list(Long spuId) {
        log.debug("开始处理[根据spuId获取sku列表]业务 参数:{}", spuId);
        return skuMapper.listBySpuId(spuId);
    }
}
